#python 3 的 print 用法是 print () 即需要带上括号
'''
#多行注释可以使用
"""注释内容"""
也可以使用前后各三个连续的单引号，中间是注释内容。
单行注释可以使用 # 。

#第一个
print ("*------------*")
print ("hello world!");
print ("hello again!");
print ("I like typing this.");
print ('Yay! Printing.');
print ("I'd much rather you 'not'.");
print ('I "said" do not touch this.');
print ('#--------*--------#');
print ("I counld have code like this.");#and the comment after is ignored

print ("I will now count my chickens:");
print ("Hens",25+30/6);#先计算除法。
print ("Roosters",100-25*3%4);#75除4取余为3，100-3为97.
print ('Now I will count the eggs:')
print (3+2+1-5+4%2-1/4+6);#1/4为0.25，4%2为0.
print (7.0/4.0,7/4);
print ("Is it true that 3+2<5-7?");
print (3+2<5-7);#5是大于-2的，因此结果为假
print ("What is 3+2?",3+2);
print ("What is 5-7?",5-7);
print ("Oh, that's why it's False.");
print ("How  about some more.");
print ("Is it greater?",5>-2);
print ("Is it greater or equal?",5>=-2);
print ("Is it less or equal?",5<=-2);
print ('#--------*--------#');

cars, space_in_a_car, driver, passengers=100, 4.0, 30, 90
car_not_drivern=cars-drivers;
cars_drivern=drivers;
carpool_capacity=cars_drivern*space_in_a_car;
average_passengers_per_car=passengers/cars_drivern;
print ('There are',cars,'cars available.');
print ('There are only',drivers,'drivers available.');
print ('There will be',car_not_drivern,'empty cars today.');
print ("We can transport",carpool_capacity,'people today.');
print ("We have",passengers,'to carpool today.');
print ('We need to put about',average_passengers_per_car,'in each car.');

#第二个
print ("*------------*")
name, age, height, weight = 'Zed A. Shaw', 35, 74, 180
eyes, teeth, hair= 'Blue', 'White', 'Brown'

print ("Let's talk about %s." % name);
print ("He's %d inches tall." % height);
print ("He's %d pounds heavy." % weight);
print ("Actually that's not too heavy.");
print ("He's got %s eyes and %s hair." %(eyes,hair));
print ("His teeth are usually %s depending on the coffee." % teeth);
print ("If I add %d, %d, and %d I get %d." %(age,height,weight,age+height+weight));
print ("将变量的英制单位转换为国际制单位后的结果如下：");
print ("Turn Height into SI(system international) is %d." % round(height*2.54));
print ("Turn Weight into SI(system international) is %f." % (weight*454/1000));
#如果""%之后的字符串的写法是 weight*454/1000 ,将会出现错误，
#因为 % 后面的字符串必须表示的是一个变量不能是式子。

x = 'There are %d types of people.' % 10;
binary, do_not= 'binary', "don't"
y = "Those who know %s and those who %s." % (binary,do_not);
print (x); print (y);
print ("I said: %r." % x);# “%r” 的功能是不管什么都打印出来！
print ("I also said: '%s'."% y);
hilarious = False;
joke_evaluation = "Isn't that joke so funny?! %r";# %r 可以将变量的原始数据显示出来。
print (joke_evaluation%hilarious);
w = "This is the left side of...";
e = "a string with a right side.";
print (w+e);#将两个字符串合并为一个字符串

#第三个
print ("*------------*")
print ('Mary had a little lab.');
print ("It's fleece was white as %s." % 'snow');# % 可以跟着字符串。
print ('And everywhere that Mary went.');
print ("."*10);#what'd that do?作用是输出 . 10次
end1,end2,end3,end4,end5,end6= 'C','h','e','e','s','e'
end7,end8,end9,end10,end11,end12= 'B','u','r','g','e','r'
print (end1+end2+end3+end4+end5+end6+' ',end='');#end的作用是用于实现输出不换行。
print (end7+end8+end9+end10+end11+end12 );

formatter = "%r %r %r %r";
print (formatter % (1,2,3,4));
print (formatter % ('one','two','three','four'));
print (formatter % (True,False,False,True));
print (formatter % (formatter,formatter,formatter,formatter));
print (formatter % (
    "I had this thing.",
    "That you could type up right.",
    "But it didn't sing.",
    "So I said goodnight."
    ));
days = "Mon Tue Wed Thu Fri Sat Sun";
months = "\nJan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug";#将字符串进行多行输出
print ("Here are the days:",days);
print ("Here are the months:",months);
print ("""
There's something going on here.
With the three double-qoutes.
We'll be able to type as much as we like.
Even 4 lines if we want,or 5,or 6.
""");

#第四个
print ("*------------*")
tabby_cat = "\tI'm tabbed in.";#输出一个tab（制表符）
persian_cat = "I'm split\non a line.";
backslash_cat = "I'm \\ a \\ cat.";#转义输出一个反斜杠\
fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t*
grass
""";    #输出一个tab（制表符）
print (tabby_cat); print (persian_cat); print (backslash_cat); print (fat_cat);
print ("%r" % fat_cat);#将会输出编程人员敲进程序中的任何字符（包括换行，转义等），输出如下：
#   "\nI'll do a list:\n\t* Cat food\n\t* Fishies\n\t* Catnip\n\t*\ngrass\n"
while True:
    for i in ["/","-","|","\\","|"]:
        print ("%s\r" % i,);# %s 不会输出逗号 ， 
        print ("%r\r"% i,); # %r 会输出逗号 ， 

print ("How old are you?",end=''); age = input();
print ("How tall are you?",end=''); height = input();
print ("How much do you weight?",end=''); weight = input();
print ("So,you're %r old,%r tall and %r heavy."%(age,height,weight));
age = input("How old are you? "); height = input("How tall are you? "); weight = input ("How much do you weight? ");
print ("So,you're %r old,%r tall and %r heavy." %(age,height,weight));

from sys import argv;
script, first, second, third ,user_name= argv
print ("The script is called：",script);
print ("Your first variable is：",first);
print ("Your second variable is：",second);
print ("Your third variable is：",third);
prompt = '>';
print ("Hi %s,I'm the %s script." % (user_name,script))
print ("I'd like to ask you a few question.")
print ("Do you like me %s?" % user_name)
likes = input(prompt)
print ("Where do you live %s?" % user_name)
lives = input(prompt)
print ("What kind of computer do you have?")
computer = input(prompt)
print ("""
Alright,so you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice.
""" % (likes,lives,computer))

#第五个
print ("*------------*")
from sys import argv
# sys 是一个软件包，argv 是sys的一个特性
# 上面这一句话的意思是从软件包中去除一个特性供人使用
script, filename= argv   
# scrip 是用于获取本源代码文件的名字的
# filename 是用于获取用户使用命令行运行本程序时，所给的参数的
txt = open(filename)
#以 txt 形式打开一个文件，并返回一个叫做“file object”的东西
#但是目标文件必须与源代码文件在同一个文件目录下
print ("Here's you file %r:" % filename)
print (txt.read())
#将文件的内容以 txt 形式输出
print ("Type the file name again:")
file_again = input(">")
# ">" 的作用是在下一行输入的时候，可以好看一些
#例如 >help.txt
txt_again = open(file_again)
print (txt_again.read())

from sys import argv
script, filename = argv
print ("We're going to erase %r." % filename)
print ("If you don't want that, hit CCTRL-C(^C).")
print ("If you do want that, hit RETURN.")
input ("?")
print ("Opening the file...")
target = open(filename,'w')
# w 表示的是写入模式
# target = open(filename) 时默认使用‘r’模式，即只读模式
print ("Truncating the file. Goodbye!")
target.truncate()
#清空已打开的文件
print ("Now I'm going to ask you for three lines.")
line1 = input('line1:')
line2 = input('line2:')
line3 = input('line3:')
print ("I'm going to write these to the file.")
target.write(line1)
#向打开的文件中写入内容
target.write("\n"); target.write(line2); target.write("\n");
target.write(line3); target.write("\n");
print ("And finally, we close it."); target.close();
#关闭已打开的文件与保存是同样的作用

from sys import argv
from os.path import exists
# exists 的作用是 如果文件存在则返回True，否则返回False
script, from_file, to_file = argv
print ("Copying from %s to %s" % (from_file,to_file))
#in_file = open(from_file)
#indata = in_file.read()
indata = open(from_file).read()
# 这一句的作用是将上面的两句合并为一句，而且执行完毕后就不需要再关闭文件了
print ("The input file is %d bytes long" % len(indata))
print ("Does the output file exist? %r" % exists(to_file))
print ("Ready, hit RETURN to continue, CTRL-C to zbort.")
input(">")
out_file = open(to_file,'w')
out_file.write(indata)
print ("Alright, all done.")
out_file.close()
#in_file.close()

#第六个
print ("*------------*")
def print_two(*args):
    arg1,arg2 = args
    print ("arg1: %r, arg2: %r" % (arg1,arg2))
# def 创建一个函数print_two,参数是*args，最后以冒号‘:’结束
def print_t_again(arg1,arg2):
    print ("arg1: %r, arg2: %r" % (arg1,arg2))
def print_one(arg1):
    print ("arg1: %r" % arg1)
def print_none():
    print ("I got nothing'.")
print_two("Zed","Shaw")
print_t_again("Zed","Shaw")
print_one("First!")
print_none()

def cheese_and_crackers(cheese_count,boxes_of_crackers):
    print ("You have %d cheeses!" % cheese_count)
    print ("You have %d boxes of crackers!" % boxes_of_crackers)
    print ("Man that's enough for a party!")
    print ("Get a blanket.\n")

print ("We can just give the function numbers directly:")
cheese_and_crackers(20,30)
print ("OR, we can use variables from our script:")
amount_of_cheese = 10
amount_of_crackers = 50
cheese_and_crackers(amount_of_cheese,amount_of_crackers)
print ("We can even do math inside too:")
cheese_and_crackers(10+20,5+6)
print ("And we can combine the two, variables and mant:")
cheese_and_crackers(amount_of_cheese+100,amount_of_crackers+1000)
def fun(a,b):
    if a>b:
        print ("%r" % a)
    else:
        print ("%r" % b)
ab = input("int>")
cd = input("int>")
fun(ab,cd)

#第七个
print ("*------------*")
from sys import argv
script,input_file = argv
def print_all(f):
    print (f.read())
    # 读取文件并输出
def rewind(f):
    f.seek(0)
    # 回到文件的开始
def print_a_line(line_count,f):
    print (line_count,f.readline())
    # f.readline() 可以读取文件的一行，并输出一个换行符
current_file = open(input_file)
print ("First let's print the whole file:\n")
print_all(current_file)
print ("Now let's rewind, kind of like a tape.")
rewind(current_file)
print ("Let's print three lines:")
current_line = 1
print_a_line (current_line,current_file)
current_line = current_line + 1
print_a_line (current_line,current_file)
current_line = current_line + 1
print_a_line (current_line,current_file)

def add(a,b):
    print ("ADDING %d + %d" %(a,b))
    return a + b
    # 函数具有返回值
def subtract(a,b):
    print ("SUBTRACTING %d - %d" %(a,b))
    return a - b
def multiply(a,b):
    print ("MULTIPLYING %d * %d" %(a,b))
    return a * b
def divide(a,b):
    print ("DIVIDING %d / %d" %(a,b))
    return a / b
print ("Let's do some math with just functings!")
age = add(30,5)
height = subtract(78,4)
weight = multiply(90,2)
iq = divide(100,2)
print ("Age: %d, Height: %d, Weight: %d, IQ: %d" %(age,height,weight,iq))
print ("Here is a puzzle.")
what = add(age, subtract(height, multiply(weight, divide(iq,2))))
print ("That becomes: ",what,"Can you do it by hand?")

#第八个
print ("*------------*")
print ("Let's practice everything.")
print ('You\'d need to know \'bount escapes with\\ that do \n newlines and \t tabs.')
poem = """
\tThe lovely world
with logic so firmly planted
cannotdiscern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
"""
print ("-------")
print (poem)
print ("-------")
five = 10-2+3-6
print ("This should be five: %s" % five)
def secret_formula(started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars /100
    return jelly_beans,jars,crates
start_point = 10000
beans, jars, crates = secret_formula(start_point)
print ("With a starting point of: %d" % start_point)
print ("We'd have %d beans, %d jars, and %d crates." %(beans,jars, crates))
start_point = start_point / 10
print ("We can also do that this way:")
print ("We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point))
'''
def break_words(stuff):
    words = stuff.split(' ')
    return words
def sort_words(words):
    return sorted(words)
def print_first_word(words):
    word = words.pop(0)
    print (word)
def print_last_word(words):
    word = words.pop(-1)
    print (word)
def sort_sentence(sentence):
    words = break_words(sentence)
    return sort_words(words)
def print_first_and_last(sentence):
    words = break_words(sentence)
    print_first_word(words)
    print_last_word(words)
def print_first_and_last_sorted(sentence):
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)

#第九个
print ("*------------*")

#第十个
print ("*------------*")








